package com.company3;

public class Maxstr {
    public static void main(String[] args) {
        //计算
        String maxSonStr = getMaxSonStr("asdafghjka", "aaasdfg");
        //判断如果为空则没有字符串，否则返回字符串
        if ("".equals(maxSonStr)) {
            System.out.println("没有最大子串");
        } else {
            System.out.println("最大子串：" + maxSonStr);
        }
    }
    //判断
    static String getMaxSonStr(String s1, String s2) {
        //判断长度大小
        String maxStr = s1.length() > s2.length() ? s1 : s2;
        String minStr = s1.length() > s2.length() ? s2 : s1;
        //初始化最大重复字符串
        String maxSonStr = "";
        //初始化判断用子串
        String sonStr = "";
        //判断长的是否包含短的，如果包含直接返回
        if (maxStr.contains(minStr))
            return minStr;
        //递减字符串长度进行匹配
        outer: for (int i = 1; i < minStr.length(); i++) {
            for (int j = 0; j <= i; j++) {
                sonStr = minStr.substring(j, minStr.length() - i + j);
                if (maxStr.contains(sonStr)) {
                    maxSonStr = sonStr;
                    break outer;
                }
            }
        }
        //如果没有匹配直接返回
        return maxSonStr;
    }
}
